package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.shop.Result.LdbzResult;
import com.shop.admin.service.AdminLoginService;
import com.shop.dao.LdbzAdminAuthMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

public class AdminLoginServiceImpl implements AdminLoginService {

    private static final Logger logger = LoggerFactory.getLogger(AdminLoginServiceImpl.class);

    private final String REDIS_KEY_VERIFYCODE = "VERIFYCODE:" ;
    private static final String REDIS_KEY_ADMIN_SESSION = "ldbz-admin-";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LdbzAdminAuthMapper ldbzAdminAuthMapper;

    @Override
    public LdbzResult userLogin(String account, String password, String code, String uid) {
        if (StringUtils.isEmpty(account)) {
            return LdbzResult.build(401,"用户名不能为空！");
        }
        if (StringUtils.isEmpty(password)) {
            return LdbzResult.build(401,"密码不能为空！");
        }
        if (StringUtils.isEmpty(code)) {
            return LdbzResult.build(401,"验证码不能为空！");
        }
        String verifycode = "";
        try {
            verifycode = (String) redisTemplate.opsForValue().get(REDIS_KEY_VERIFYCODE+uid);
        }catch (Exception e){
            logger.error("Reids错误",e);
        }
        if (StringUtils.isBlank(verifycode)) {
            return LdbzResult.build(400,"图片验证码已过期，请重新获取");
        }
        if (!verifycode.equalsIgnoreCase(code)) {
            return LdbzResult.build(400,"验证码输入有误！");
        }
        QueryWrapper qw = new QueryWrapper();
        qw.eq("",account);
        ldbzAdminAuthMapper.selectList();
        return null;
    }
}
